# Maintainer: Patrick Stewart <patstew@gmail.com>

_realname=qtconsole
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=4.2.1
pkgrel=1
pkgdesc="A rich Qt-based console for working with Jupyter kernels, supporting rich media output, session export, and more. (mingw-w64)"
arch=('any')
url="http://jupyter.org/"
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools")

source=("${_realname}-${pkgver}.tgz::https://github.com/jupyter/qtconsole/archive/${pkgver}.tar.gz")
md5sums=('97a3e29cd69bc0270411b313c907c173')

all_build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/${1} setup.py install \
    --prefix=${MINGW_PREFIX} --root=${pkgdir} --optimize=0

  find "${pkgdir}/" -name "*.pyc" -delete
  find "${pkgdir}/" -type d -empty -delete

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*; do
    sed -e "s|${PREFIX_WIN}/bin/|/usr/bin/env |g" -i ${_f}
  done
}

package_python3-qtconsole() {
  local interpreter=python3
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-jupyter_core"
           "${MINGW_PACKAGE_PREFIX}-python3-jupyter_client"
           "${MINGW_PACKAGE_PREFIX}-python3-pyqt5")

  all_build ${interpreter}

  install -Dm644 "COPYING.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}

package_python2-qtconsole() {
  local interpreter=python2
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-jupyter_core"
           "${MINGW_PACKAGE_PREFIX}-python2-jupyter_client"
           "${MINGW_PACKAGE_PREFIX}-python2-pyqt5")

  all_build ${interpreter}

  install -Dm644 "COPYING.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"

  cd "${pkgdir}${MINGW_PREFIX}"/bin
  for _f in *; do
    mv ${_f} ${_f/%.*/}2${_f/${_f/%.*/}/}
  done
}

package_mingw-w64-i686-python2-qtconsole() {
  package_python2-qtconsole
}

package_mingw-w64-i686-python3-qtconsole() {
  package_python3-qtconsole
}

package_mingw-w64-x86_64-python2-qtconsole() {
  package_python2-qtconsole
}

package_mingw-w64-x86_64-python3-qtconsole() {
  package_python3-qtconsole
}
